﻿using System;
using System.Collections.Generic;
using System.Reflection;
using UtilZ.Dotnet.Ex.Json;

namespace ConAppCore.Json
{
    public class Person
    {
        public long Id { get; set; }

        public string Name { get; set; }

        public double DoubleValue1 { get; set; }
        public double DoubleValue2 { get; set; }
        public double DoubleValue3 { get; set; }

        public string DetailHtml { get; set; }

        [JsonProperty("Age", ValueConverterType = typeof(AgeJsonValueConverter))]
        public int Age { get; set; }

        public ulong Age2 { get; set; }
        public int Age3 { get; set; }

        public double DoubleValue { get; set; }

        public string ReadOnlyValue1 { get; set; }
        public string ReadOnlyValue { get; set; }

        public object IntValue { get; set; }
        public object StrValue { get; set; }
        public object DateTimeValue { get; set; }

        public Dictionary<string, int> DicInt { get; set; }

        public Dictionary<string, object> DicObj { get; set; }

        public Dictionary<int, string> DicObj2 { get; set; }

        [JsonProperty("ObjList", 11, Description = "object列表\n第一个比\n第二个")]
        public List<object> ObjList { get; set; }

        public List<string> StrList { get; set; }

        [JsonProperty("IntList", 11, MemberIgnoreerType = typeof(PersonIntListMemberIgnoreer))]
        public List<int> IntList { get; set; }

        public static Person CreateDemo()
        {
            string speStr = "{成都\"a:b\\c,c}";
            //speStr = "成都abc";

            var dic = new Dictionary<string, object>();

            return new Person()
            {
                Id = 100,
                Name = speStr,
                Age = 22,
                ReadOnlyValue = "ReadOnlyValue",
                IntValue = 123,
                StrValue = "慧汇款",
                DateTimeValue = DateTime.Parse("2000-04-05 12:25:36"),
                DicInt = new Dictionary<string, int>()
                {
                     {"key1",100 },
                      {"key2",200 },
                      {"key3",300 },
                },
                DicObj = new Dictionary<string, object>()
                {
                    {"intKey",456 },
                    {"strKey","中国" },
                    {"dateTimeKey",DateTime.Parse("2000-04-06 18:36:45") },
                    {"objKey",123 },
                    {"boolKey",true },
                },
                DicObj2 = new Dictionary<int, string>()
                {
                    {456 ,"intKey"},
                    {325,"中国" },
                },
                ObjList = new List<object>()
                {
                    789,"中国",DateTime.Parse("2000-04-06 18:36:45"),true
                },
                StrList = new List<string>() { "张三", "李四", "王五" },
                IntList = new List<int>() { 1000, 2000, 3000 }
            };
        }
    }

    internal class PersonIntListMemberIgnoreer : IJsonMemberIgnoreer
    {
        public PersonIntListMemberIgnoreer()
        {

        }

        public bool Ignore(MemberInfo memberInfo, object owner, object value)
        {
            if (!(owner is Person))
            {
                return false;
            }

            if (!string.Equals(nameof(Person.IntList), memberInfo.Name))
            {
                return false;
            }

            var intList = (List<int>)value;
            if (intList.Contains(2000))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    public class AgeJsonValueConverter : JsonValueConverterAbs
    {
        private readonly int _rnd = 1000;
        /// <summary>
        /// 构造函数
        /// </summary>
        public AgeJsonValueConverter()
            : base(false)
        {

        }





        /// <summary>
        /// 尝试将对象值转换为json值[转换成功返回true,失败返回false]
        /// </summary>
        /// <param name="memberInfo">成员信息</param>
        /// <param name="valueType">值类型</param>
        /// <param name="owner">成员所属对象</param>
        /// <param name="value">属性或字段值</param>
        /// <param name="jsonValue">成员值</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        protected override bool PrimitiveTryConvertTo(MemberInfo memberInfo, Type valueType, object owner, object value, out string jsonValue)
        {
            int age = (int)value;
            jsonValue = (age + this._rnd).ToString();
            return true;
        }

        /// <summary>
        /// 尝试将json值转换为对象值[转换成功返回true,失败返回false]
        /// </summary>
        /// <param name="memberInfo">成员信息</param>
        /// <param name="valueType">值类型</param>
        /// <param name="owner">成员所属对象</param>
        /// <param name="jsonValue">json值</param>
        /// <param name="value">对象值</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        protected override bool PrimitiveTryConvertFrom(MemberInfo memberInfo, Type valueType, object owner, string jsonValue, out object value)
        {
            bool result = true;

            if (string.IsNullOrWhiteSpace(jsonValue))
            {
                if (JsonHelper.AllowNull(valueType))
                {
                    //非可空类型,但是值为null,不符合预期
                    result = false;
                    value = null;
                }
                else
                {
                    value = null;
                }
            }
            else
            {
                int age;
                result = int.TryParse(jsonValue, out age);
                if (result)
                {
                    value = age - this._rnd;
                }
                else
                {
                    //json值不是有效的TimeOnly格式值
                    value = null;
                }
            }

            return result;
        }
    }
}
